package top.kent90.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import top.kent90.common.controller.BaseController;
import top.kent90.common.object.LoginObject;
import top.kent90.common.util.MD5Util;
import top.kent90.common.util.VerifyCode;
import top.kent90.pojo.ZczyUser;
import top.kent90.service.LoginService;
import top.kent90.service.RedisService;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("login")
public class LoginController extends BaseController {

    //@Autowired通过dubbo服务提供bean注入(idea报红，不用管)
    @Reference
    LoginService loginService;

    @Reference
    RedisService redisService;

    @RequestMapping("doLogin")
    public String doLogin(String mobile, String user_pass,
                          HttpServletRequest request, Model model) {


        //后端校验
        System.out.println("----------进入校验流程-----------");

        //1. 手机号填写没有
        // user_phone!=null&&!"".equals(user_phone)
        System.out.println("----------开始校验是否输入手机号-----------");
        if(StringUtils.isBlank(mobile)) {
            model.addAttribute("msg", "请输入手机号!");
            return "login";
        }
        //2.  密码填写没有
        System.out.println("----------已输入手机号，开始校验是否输入密码-----------");
        if(StringUtils.isBlank(user_pass)) {
            model.addAttribute("msg", "请输入密码!");
            model.addAttribute("mobile", mobile);
            return "login";
        }
//        //3.  验证码没有
//        if(StringUtils.isBlank(verifyCode)) {
//            model.addAttribute("msg", "请输入验证码!");
//            //数据带回
//            model.addAttribute("mobile", mobile);
//            model.addAttribute("user_pass", user_pass);
//            return "../../index";
//        }
//
//        Object obj = request.getSession().getAttribute("verifyCode");
//        if(obj==null) {
//            model.addAttribute("msg", "会话超时，请重新登录!");
//            return "../../index";
//        }
//        String sessionVerifyCode = (String)obj;
//        //4  验证码错误
//        if(!sessionVerifyCode.equalsIgnoreCase(verifyCode)) {
//            model.addAttribute("msg", "验证码错误!");
//            //数据带回
//            model.addAttribute("mobile", mobile);
//            model.addAttribute("user_pass", user_pass);
//            return "../../index";
//        }

        //开始查询用户信息
        System.out.println("----------校验完成，开始查询用户信息-----------");
        ZczyUser zczyUser = null;
        try {
            //开始查询用户信息
            zczyUser = loginService.queryByMobile(mobile);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("-------查询失败--------");
            return "login";
        }
        //控制台查看查询到的用户信息
        System.out.println("----------查询成功--------------");
        System.out.println("zczyUser------"+zczyUser);
        //用户不存在
        if(zczyUser==null) {
            model.addAttribute("msg", "用户不存在，请确认!");
            return "login";
        }
        //密码错误
        if(!MD5Util.encode(user_pass).equals(zczyUser.getUserPass())) {
            model.addAttribute("msg", "密码错误!");
            return "login";
        }

        //把查询到的数据封主到loginObject对象中
        LoginObject loginObject = new LoginObject();
        loginObject.setUserId(zczyUser.getId());
        loginObject.setPhone(zczyUser.getMobile());
        loginObject.setUserName(zczyUser.getUserNicename());
        loginObject.setLoginName(zczyUser.getUserLogin());
        loginObject.setLoginPwd(zczyUser.getUserPass());

        //把loginObject 对象放入session中
        HttpSession session = request.getSession();
        session.setAttribute("loginObject",loginObject);

        //将用户身份信息保存在redis中，并设置过期时间为10分钟
        String sessionId=session.getId();
        System.out.println("sessionId:"+sessionId);
        redisService.set(sessionId, loginObject, 600);
        return "main";
    }

    /**
     * 输出验证码
     * @param request
     * @param response
     */
    @GetMapping("verify")
    public void verifyCode(HttpServletRequest request,
                           HttpServletResponse response) {
        VerifyCode.exportImg(request, response, 200, 69);
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "../../index";
    }
}
